package com.sgb.kjwl.view.ui.activity.quotedprice;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityQuotedPriceBinding;
import com.sgb.kjwl.viewmodel.packers.LaborServiceAndEmploymentViewModel;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.TeamPriceItemEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.packers.TeamOfferDetailActivity;
import com.sgb.kjwl.view.ui.adapter.price.QuotePriceListAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.ThreeLevelFilterWindow;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.sgb.kjwl.viewmodel.quotedprice.QuotedPriceModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
*@author zhangwl
*@date 2020/6/14
*description:工种报价列表
*/
public class QuotedPriceActivity extends AppActivity {

    public QuotedPriceModel viewModel;
    private ActivityQuotedPriceBinding binding;
    private QuotePriceListAdapter adapter;

    /**工程分类*/
    private ThreeLevelFilterWindow popCategory;
    private PickResultEntity pickResultProject1;
    private PickResultEntity pickResultProject2;
    private PickResultEntity pickResultProject3;
    /**工种名称*/
    private PickPopWindow1 popWorkType;
    private PickResultEntity pickResultWorkType;
    /**所在区域*/
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;
    private AddressEntity mAddress;

    public static void start(Context context, String id, String title) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.putExtra("title", title);
        intent.setClass(context, QuotedPriceActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        super.setupActivityComponent(appComponent);

    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getList(pickResultProject1.getNo(), pickResultProject2.getNo(), pickResultProject3.getNo(),
                pickResultWorkType.getNo(), pickResultProvince.getNo(), pickResultCity.getNo(), pickResultArea.getNo(), isLoadMore);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_quoted_price);
        viewModel = new ViewModelProvider(this).get(QuotedPriceModel.class);
        if (adapter == null){
            adapter = new QuotePriceListAdapter(getActivity(), null);
        }

        adapter.setOnItemClick(new AdapterOnItemClick<TeamPriceItemEntity>() {
            @Override
            public void onItemClick(TeamPriceItemEntity teamPriceItemEntity, int position) {
                TeamOfferDetailActivity.start(getActivity(), teamPriceItemEntity.getNo());
            }
        });

        binding.list.setLayoutManager(new XLinearLayoutManager(this));
        binding.list.addItemDecoration(new RecyclerCustomDecoration(
                0,
                0,
                0,
                getResources().getDimension(R.dimen.dp10)));
        binding.list.setAdapter(adapter);
        binding.list.setLoadingMoreEnabled(true);
        binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        pickResultProject1 = new PickResultEntity();
        pickResultProject2 = new PickResultEntity();
        pickResultProject3 = new PickResultEntity();
        pickResultWorkType = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();

        mAddress = (AddressEntity) getIntent().getSerializableExtra("entity");
        if (mAddress != null){
            pickResultProject1.setNo((mAddress).getNo());
            pickResultProject1.setName((mAddress).getName());

        }
    }

    @Override
    protected void init() {
        initFilterData(false);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_project) {
            if (popCategory == null) {
                viewModel.getCategoryType();
            } else {
                showCategoryPop(null);
            }
        } else if (id == R.id.tv_work_type) {
            if (popWorkType == null) {
                viewModel.getWorkType();
            } else {
                showWorkTypePopWindow(null);
            }
        } else if (id == R.id.tv_area) {
            if (popCity == null) {
                viewModel.getCityList();
            } else {
                showCityPop(null);
            }
        }
    }

    public void showList(List<TeamPriceItemEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.list.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.list.setNoMore(isLast);
        } else {
            binding.list.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.list.smoothScrollToPosition(0);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    //工程-工程分类
    public void showCategoryPop(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        if (popCategory == null) {
            popCategory = new ThreeLevelFilterWindow(getActivity());
            popCategory.setBgView(binding.grayLayout);
            if (mAddress != null){
                int position = 0;
                for (int i = 0; i < data.size(); i++) {
                    if (mAddress.getNo().equals(data.get(i).getNo())){
                        position = i;
                        break;
                    }
                }
                popCategory.setDefaultItem(position + 1);
            }
            popCategory.setOnResultClick(new ThreeLevelFilterWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultProject1.setNo(((PickResultEntity) key1).getNo());
                    pickResultProject1.setName(((PickResultEntity) key1).getName());
                    pickResultProject2.setNo(((PickResultEntity) key2).getNo());
                    pickResultProject2.setName(((PickResultEntity) key2).getName());
                    pickResultProject3.setNo(((PickResultEntity) key3).getNo());
                    pickResultProject3.setName(((PickResultEntity) key3).getName());
                    initFilterData(false);
                }

                @Override
                public void reset() {
                    pickResultProject1.clear();
                    pickResultProject2.clear();
                    pickResultProject3.clear();
                    initFilterData(false);
                }
            });
            popCategory.setOnDismissListener(() -> {
            });
            popCategory.setThreeLevelData(data);
        }

    }

    //工种名称pop
    public void showWorkTypePopWindow(List<PickSelectEntity> data) {
        if (popWorkType == null) {
            popWorkType = new PickPopWindow1(this);
            popWorkType.setBgView(binding.grayLayout);
            popWorkType.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                pickResultWorkType.setNo((String) key1);
                pickResultWorkType.setName((String) key2);
                pickResultWorkType.setPosition((int) key3);

                initFilterData(false);
            });
            popWorkType.setOnDismissListener(() -> {

            });

            popWorkType.setData(data);
        }

    }

    //省市区pop
    public void showCityPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (popCity == null) {
            popCity = new ThreeLevelAreaWindow(getActivity()){
                @Override
                protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.provinceClick(baseAreaEntity, position);
                    pickResultProvince.setNo(baseAreaEntity.getNo());
                    pickResultProvince.setName(baseAreaEntity.getSimpleName());
                    if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultCity.setNo("");
                        pickResultCity.setName("");
                    }
                }

                @Override
                protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.cityClick(baseAreaEntity, position);
                    pickResultCity.setNo(baseAreaEntity.getNo());
                    pickResultCity.setName(baseAreaEntity.getSimpleName());
                    if ("全部".equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.areaClick(baseAreaEntity, position);
                    pickResultArea.setNo(baseAreaEntity.getNo());
                    pickResultArea.setName(baseAreaEntity.getSimpleName());
                }
            };
            popCity.setBgView(binding.grayLayout);
            popCity.setShowSimpleName(true);
            popCity.setProvinceSingle();
            popCity.setCitySingle();
            popCity.setAreaSingle();
            popCity.setOnResultClick((key1, key2, key3) -> {
                initFilterData(false);

            });
            popCity.setOnDismissListener(() -> {

            });

        }

    }

    public void getUrl(String url){
        WebViewX5HostActivity.start(QuotedPriceActivity.this,url);
    }
}